package com.jimmy.medical.service.impl;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.enums.PictureUseTypeEnum;
import com.jimmy.medical.model.SignInfo;
import com.jimmy.medical.service.ISignFlowOrderService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Qualifier("signFlowOrderService")
public class SignFlowOrderServiceImpl extends FlowOrderServiceImpl
        implements ISignFlowOrderService {

    @Override
    public Long saveAffirmInfo(Long orderId, Long userId) {
        FlowOrder flowOrder = process(orderId, userId, "确认接单", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveAffirmDial(Long flowOrderId,Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "致电客户", 2);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话告知", 4);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getOrderId(), message.getUserId(), "短信告知", 5);
        return getCountDown(flowOrder);
    }

    @Override
    public FlowOrder submitSign(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成挂号", 6);

        //生成下一个工单,转入中台指派
        return getNextFlowOrder(flowOrder);
    }

    @Override
    public SignInfo getSignInfo(Long flowOrderId) {
        SignInfo info = new SignInfo();

        FlowOrder flowOrder = repository.findOne(flowOrderId);
        Order order = flowOrder.getOrder();
        info.setOrderId(order.getId());
        info.setDate(order.getOrderDate());
        info.setTime(order.getOrderTime());
        info.setUserId(flowOrder.getUser().getId());
        info.setSignHospital(order.getHospital());
        info.setSignDepartment(order.getDepartment());
        info.setSignDoctor(order.getDoctor());
        info.setAdvanceFee(order.getPayAmount().doubleValue());
        info.setReimburseFee(order.getCostAmount().doubleValue());

        List<UploadFile> advanceVoucherList = uploadFileRepository.findAllByRelatedKeyAndUseType(order.getId().toString(), PictureUseTypeEnum.ADVANCE_FEE.getKey());
        info.setAdvanceVoucherList(advanceVoucherList);

        List<UploadFile> reimburseVoucherList = uploadFileRepository.findAllByRelatedKeyAndUseType(order.getId().toString(), PictureUseTypeEnum.REIMBURSE_FEE.getKey());
        info.setReimburseVoucherList(reimburseVoucherList);

        return info;
    }
}